# -*- coding: utf-8 -*-

import sys

def dump(obj):
  '''return a printable representation of an object for debugging'''
  newobj=obj
  if '__dict__' in dir(obj):
    newobj=obj.__dict__
    if ' object at ' in str(obj) and not newobj.has_key('__type__'):
      newobj['__type__']=str(obj)
    for attr in newobj:
      newobj[attr]=dump(newobj[attr])
  return newobj

def listar(lista, cols):

	for list_item in cols:
		sys.stdout.write( '+' )
		for i in range(list_item['chars']):
			sys.stdout.write( '-' )

		sys.stdout.write( '--' )

	sys.stdout.write( '+' )
	sys.stdout.write( '\n' )

	for list_item in cols:
		sys.stdout.write( '|' )

		value = list_item['name']
		if 'title' in list_item:
			value = list_item['title']

		chars = list_item['chars']

		align = 'left'
		if 'align' in list_item:
			align = list_item['align']

		if align == 'left':
			value = value.ljust( chars )
		if align == 'right':
			value = value.rjust( chars )

		sys.stdout.write( ' ' + value + ' ' )

	sys.stdout.write( '|' )
	sys.stdout.write( '\n' )

	for list_item in cols:
		sys.stdout.write( '+' )
		for i in range(list_item['chars']):
			sys.stdout.write( '-' )
		sys.stdout.write( '--' )

	sys.stdout.write( '+' )
	sys.stdout.write( '\n' )

	#import string
	#valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits)

	import unicodedata


	for s in lista:
		for list_item in cols:
			sys.stdout.write( '|' )
			value = s.__dict__[ list_item['name'] ]
			chars = list_item['chars']
			if len(value) > chars:
				value = s.__dict__[ list_item['name'] ][:chars-4] + ' ...'

			try:
				#value = ''.join(c for c in value if c in valid_chars)
				value = ''.join((c for c in unicodedata.normalize('NFD', unicode(value, 'utf-8') ) if unicodedata.category(c) != 'Mn'))
                        	#value = ''.join((c for c in unicodedata.normalize('NFD', value.encode( "utf8" ) ) if unicodedata.category(c) != 'Mn'))
			except:
				value = value = ''.join((c for c in unicodedata.normalize('NFD', value ) if unicodedata.category(c) != 'Mn'))

			align = 'left'
			if 'align' in list_item:
				align = list_item['align']
			if align == 'left':
				value = value.ljust( chars )

			if align == 'right':
				value = value.rjust( chars )

			sys.stdout.write( ' ' + value +  ' ')

		sys.stdout.write( '|' )
		sys.stdout.write( '\n' )

	for list_item in cols:
		sys.stdout.write( '+' )
		for i in range(list_item['chars']):
			sys.stdout.write( '-' )
		sys.stdout.write( '--' )

	sys.stdout.write( '+' )
	sys.stdout.write( '\n' )


def detallar(object, filas, width_tit, width_data):
  
	sys.stdout.write( '+' )
	for i in range(width_tit):
		sys.stdout.write( '-' )  
	sys.stdout.write( '+' )
	for i in range(width_data):
		sys.stdout.write( '-' )  
	sys.stdout.write( '+' )
	
	for list_item in filas:
	  
	  	sys.stdout.write( '\n' )
		sys.stdout.write( '|' )

		value = list_item['name']
		if 'title' in list_item:
			value = list_item['title']

		chars = width_tit - 2

		align = 'right'
		if 'align' in list_item:
			align = list_item['align']

		if align == 'left':
			value = value.ljust( chars )
		if align == 'right':
			value = value.rjust( chars )

		sys.stdout.write( ' ' + value + ' ' )
		
		sys.stdout.write( '|' )
		
		value = object.__dict__[ list_item['name'] ]
		value = value.ljust( width_data - 2 )
		sys.stdout.write( ' ' + value + ' ' )
		
		sys.stdout.write( '|' )
	
	sys.stdout.write( '\n' )
	sys.stdout.write( '+' )
	for i in range(width_tit):
		sys.stdout.write( '-' )  
	sys.stdout.write( '+' )
	for i in range(width_data):
		sys.stdout.write( '-' )  
	sys.stdout.write( '+' )	
	sys.stdout.write( '\n' )
	pass

from base import mcBase
from cuevana import cuevanaMovie
from pprint import pprint


#mcBase init
mcBase.init()


"""
print "Retrieving:\n"
o = cuevanaMovie('4282')
pprint(o.__dict__)

print "\nSaving ...\n"
o.dbStore()

o = cuevanaMovie()
o.id = 4282
print "\nBefore Restored:\n"
pprint(o.__dict__)
o.dbLoad()
print "\Restored:\n"
pprint(o.__dict__)
"""

"""
#Test Listing Featured
feats = cuevanaMovie.featured()
listar(feats,  [{'name': 'id',  "chars" :  5 , 'align' : 'right' , 'title' : 'Ref' }, {'name': 'title',  "chars" :  45 , 'title' : 'Titulo' }, {'name': 'url',  "chars" :  60 , 'title' : 'URL' }] )

for o in feats:
	detallar(o,  [{'name': 'id',  'title' : 'ID' }, {'name': 'title',  'title' : 'Titulo' }, {'name': 'original_title',  'title' : 'Titulo Original' }, {'name': 'description',  'title' : 'Desc' },{'name': 'url',  'title' : 'URL' },{'name': 'cover_url',  'title' : 'Cover' }], 20, 60 )
	listar(o.sources,  [{'name': 'id',  "chars" :  5 , 'align' : 'right' , 'title' : 'Id' }, {'name': 'url',  "chars" :  45 , 'title' : 'URL' }, {'name': 'definition',  "chars" :  5 , 'title' : 'Def' }, {'name': 'audio_lang',  "chars" :  5 , 'title' : 'Audio' }] )
"""
	
	
"""
#Test Retrieving
o = cuevanaMovie('4277')
detallar(o,  [{'name': 'id',  'title' : 'ID' }, {'name': 'title',  'title' : 'Titulo' }, {'name': 'original_title',  'title' : 'Titulo Original' }, {'name': 'description',  'title' : 'Desc' },{'name': 'url',  'title' : 'URL' },{'name': 'cover_url',  'title' : 'Cover' }], 20, 60 )


#o.loadSources()
listar(o.sources,  [{'name': 'id',  "chars" :  5 , 'align' : 'right' , 'title' : 'Id' }, {'name': 'url',  "chars" :  45 , 'title' : 'URL' }, {'name': 'definition',  "chars" :  5 , 'title' : 'Def' }, {'name': 'audio_lang',  "chars" :  5 , 'title' : 'Audio' }] )
"""
#Test Listing Featured
feats = cuevanaMovie.featured()
listar(feats,  [{'name': 'id',  "chars" :  5 , 'align' : 'right' , 'title' : 'Ref' }, {'name': 'title',  "chars" :  45 , 'title' : 'Titulo' }, {'name': 'url',  "chars" :  60 , 'title' : 'URL' }] )


"""
o = noootvMovie("the future")
pprint(o.__dict__)
"""


"""
import json
result = json.loads('{"__complex__": true, "real": 1, "imag": 2}')
#result = json.loads("{'_mcData': '<base.mcData instance at 0xb74393cc>', 'source': 'cuevana'}")
result = json.loads('[{"_mcData": "<base.mcData instance at 0xb74393cc>"}]')

print result[0]['_mcData']

for attr in result:
	for attr2 in attr:
		print attr2

print result('_mcData')
"""

